package com.ease.map.handler.feature.draw {
    import com.ease.map.events.EditEvent;
    import com.ease.map.feature.Feature;
    import com.ease.map.geometry.IGeometry;
    import com.ease.map.geometry.PointGeometry;
    import com.ease.map.geometry.basetype.Pixel;
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    /**
     *编辑时的描点
     */
    public class AnchorPoint extends Sprite {
        /**描点坐标*/
        public var geoPoint:PointGeometry = null;

        /**锚点对应的基础图形*/
        public var parentGeo:IGeometry = null;

        /**当前锚点所在的基础图形对应的点的顺序,从0开始*/
        public var pointIndex:int = -1;
        /**锚点对应的基础图形*/
        public var parentFeature:Feature = null;

        private var pointDown:PointGeometry;
        private var pointUp:PointGeometry;

        private var editAnchor:AnchorPoint;
        private var _startPoint:Pixel = new Pixel();
        private var isEditMove:Boolean = false;
        private var isEditing:Boolean = false;

        public function AnchorPoint() {
            super();
            this.graphics.clear();
            this.graphics.beginFill(0x999999);
            this.graphics.lineStyle(1, 0x000000);
            this.graphics.drawRect(-5, -5, 10, 10);
            this.graphics.moveTo(-5, -5);
            this.graphics.lineTo(5, 5);
            this.graphics.moveTo(-5, 5);
            this.graphics.lineTo(5, -5);
            this.addEventListener(MouseEvent.MOUSE_DOWN, anchorMouseDown);
            this.addEventListener(MouseEvent.MOUSE_UP, anchorMouseUp);
            this.addEventListener(MouseEvent.MOUSE_OVER, anchorMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT, anchorMouseOut);
        }

        public function setEditMode(isEdit:Boolean = true):void {
            if (isEdit) {
                isEditing = true;
                this.graphics.clear();
                this.graphics.beginFill(0x00FF00);
                this.graphics.lineStyle(1, 0x000000);
                this.graphics.drawRect(-5, -5, 10, 10);
                this.graphics.moveTo(-5, -5);
                this.graphics.lineTo(5, 5);
                this.graphics.moveTo(-5, 5);
                this.graphics.lineTo(5, -5);
            }
            else {
                isEditing = false;
                this.graphics.clear();
                this.graphics.beginFill(0x999999);
                this.graphics.lineStyle(1, 0x000000);
                this.graphics.drawRect(-5, -5, 10, 10);
                this.graphics.moveTo(-5, -5);
                this.graphics.lineTo(5, 5);
                this.graphics.moveTo(-5, 5);
                this.graphics.lineTo(5, -5);
            }

        }

        private function anchorMouseDown(event:MouseEvent):void {
            this.parentFeature.layer.map.dispatchEvent(new EditEvent(EditEvent.ANCHORPOINT_DOWN, null, this));
        }

        private function anchorMouseUp(event:MouseEvent):void {
            this.parentFeature.layer.map.dispatchEvent(new EditEvent(EditEvent.ANCHORPOINT_UP, null, this, null));
        }

        private function anchorMouseOver(event:MouseEvent):void {
            this.graphics.clear();
            this.graphics.beginFill(0x00FF00);
            this.graphics.lineStyle(1, 0x000000);
            this.graphics.drawRect(-5, -5, 10, 10);
            this.graphics.moveTo(-5, -5);
            this.graphics.lineTo(5, 5);
            this.graphics.moveTo(-5, 5);
            this.graphics.lineTo(5, -5);
            this.parentFeature.layer.map.dispatchEvent(new EditEvent(EditEvent.VERTEX_MOUSE_OVER, null, this, null));
        }

        private function anchorMouseOut(event:MouseEvent):void {
            if (!isEditing) {
                this.graphics.clear();
                this.graphics.beginFill(0x999999);
                this.graphics.lineStyle(1, 0x000000);
                this.graphics.drawRect(-5, -5, 10, 10);
                this.graphics.moveTo(-5, -5);
                this.graphics.lineTo(5, 5);
                this.graphics.moveTo(-5, 5);
                this.graphics.lineTo(5, -5);
                this.parentFeature.layer.map.dispatchEvent(new EditEvent(EditEvent.VERTEX_MOUSE_OUT, null, this, null));
            }
        }

    }
}